package com.yun.platform.es.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ESTs {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    //必须小写
    private  static  String index_t= "device_p123123_d001_event";


    @Test
    public void create_index(){ //创建索引
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of(index_t));
        if (indexOperations.exists()){
            indexOperations.delete();
            System.out.println("删除成功！");
        }
        indexOperations.create();

    }

    @Test
    public void create_index2(){
        //创建对象索引
        // 创建索引，会根据Blog类的@Document注解信息来创建
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(UserIndex.class);

        // 配置映射，会根据Item类中的id、Field等字段来自动完成映射
/*        boolean mapping = indexOperations.putMapping(UserIndex.class);
        System.out.println("mapping创建" + mapping);*/

        //索引是否存在
        IndexOperations indexOperations2 = elasticsearchRestTemplate.indexOps(UserIndex.class);
        Map<String, Object> mapping2 = indexOperations2.getMapping();
        System.out.println("Book mapping:" + mapping2);


    }
    @Test
//    TODO 添加文档 Document
    public void addDoc(){
        UserIndex userIndex = new UserIndex(null, "秃头修炼手册", 998, "你，渴望力量吗？");
        UserIndex save = elasticsearchRestTemplate.save(userIndex);
        System.out.println(save);
    }

}
